#   Copyright (C) 2008      Tri Tech Information Systems Inc.
# 
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
# 
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
# 
#   You should have received a copy of the GNU General Public License along
#   with this program; if not, write to the Free Software Foundation, Inc.,
#   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#   
from __future__ import absolute_import
"""
Provides Distutils commands
"""
from distutils.command import build_ext
from distutils.core import Command
from distutils.dist import Distribution

import os.path
import sys

from pyxx.context import WrapperContext

import doctest


class pyxx_build_ext(build_ext.build_ext):
    """
    This class is used in the setup script in order
    to build the extension. We provide tools in order to generate
    the wrappers
    """

    def swig_sources(self, sources, extension):
        new_sources = []
        pyxx_sources = []
        pyxx_targets = {}

        for source in sources:
            (base, ext) = os.path.splitext(source)

            if ext == ".pyx":
                basename = base +".cpp"
                newpath = os.path.join(self.build_temp, basename)
                try:
                    os.makedirs(os.path.dirname(newpath))
                except:
                    pass


                new_sources.append(newpath)
                pyxx_sources.append(source)
                pyxx_targets[source] = new_sources[-1]
            else:
                new_sources.append(source)

        
        for source in pyxx_sources:
            target = pyxx_targets[source]

            ctx = WrapperContext()
            ctx.module = extension.name

            for include_dir in extension.include_dirs:
                ctx.AddIncludePath(include_dir)

            ctx.evaluate(source)

            open(target,"w").write( ctx.generate() )


        return new_sources


class run_doctest(Command):
    description = "Run doctests for this project"

    user_options = [
        ('build-dir=','b', "build directory (where to install from)"),
    ]


    def initialize_options (self):
        self.build_lib = None
        self.doctest_files = None

    # initialize_options()


    def finalize_options (self):
        self.set_undefined_options('build',
                                   ('build_lib', 'build_lib'))
        self.doctest_files = self.distribution.doctest_files

    # finalize_options()

    def run (self):
        self.run_command("build")    
        old_sys_path = sys.path[:]
        try:
            sys.path.append( self.build_lib )
            for file in self.doctest_files:
                
#                file = os.path.abspath(file)
                doctest.testfile(file, False, optionflags = doctest.ELLIPSIS)

        finally:
            sys.path = old_sys_path

        


    # run()
Distribution.doctest_files = None
#
